//
//  ROInquiryView.m
//  Runone
//
//  Created by runone02 on 16/11/16.
//  Copyright © 2016年 runone. All rights reserved.
//

#import "ROInquiryView.h"
#import "Masonry.h"
#import "AppConstants.h"
#import "AppDelegate.h"
#import "HighWayRoadRecordDirectionInfoModel.h"

#import "DrowDownViewController.h"
#import "DropDownBackView.h"
#import "UIView+Common.h"
#import "AppConstants.h"
@interface ROInquiryView ()

// **************************************** -数据(来自大接口)-****************************************

@property (nonatomic, strong) NSMutableArray *roadNameArray;        //道路名字数组
@property (nonatomic, strong) NSMutableArray *roadDirectionArray;   // 对应道路类型的方向类型数组
@property (nonatomic, strong) NSMutableArray *roadDirectionNumArray;// 1：往广州方向 2：往湖南方向 3:双向

@property (nonatomic,   copy) NSString       *defaultRoadStr;

@property (nonatomic, strong) NSArray        *transTypeArray;       //车辆类型

// **************************************** -弹出的下拉框-****************************************
@property (nonatomic, strong) DropDownBackView *dropBackV; //下拉框



@end

@implementation ROInquiryView


- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    _dropBackV               = [[DropDownBackView alloc] init];
    AppDelegate *mydelegate                 = (AppDelegate *)[UIApplication sharedApplication].delegate;
    _currentRoadModel                       =  mydelegate.HighWayRoadArray[0];
    self.backgroundColor     = [UIColor colorWithRed:100.0 / 255 green:100.0 / 255 blue:100.0 / 255 alpha:0.8];
    [self setUpArray];
    [self configUI];
    [self layoutUI];
    [self btnClick];
    [self configDefatulArrayData];
    [self setDefaultShowData];
    return self;
}

-(void) setUpArray {
    _roadDirectionNumArray = [[NSMutableArray alloc] init];
    _roadDirectionArray    = [[NSMutableArray alloc] init];
    _roadNameArray         = [[NSMutableArray alloc] init];
    
}
- (void) configUI {
    _containerView = [[UIView alloc] init];
    _containerView.backgroundColor = [UIColor colorWithRed:239.0/255 green:239.0/255 blue:244.0/255 alpha:1];
    _containerView.layer.cornerRadius  = 10;
    _containerView.layer.masksToBounds = YES;
    [self addSubview:_containerView];
    
    _roadL      = [[UILabel alloc] init];
    _roadL.text = @"道路类型：";
    _roadL.font = KTitleTextFont;
    [_containerView addSubview:_roadL];
    
    _roadTF                   = [[UITextField alloc] init];
    _roadTF.borderStyle       = UITextBorderStyleNone;
    _roadTF.layer.borderColor = [[UIColor lightGrayColor] CGColor];
    _roadTF.layer.borderWidth = 1.0;
    [_containerView addSubview:_roadTF];
    
    _roadBtn = [[UIButton alloc] init];
    [_containerView addSubview:_roadBtn];
    
    _transL         = [[UILabel alloc] init];
    _transL.text    = @"车辆类型：";
    _transL.font    = KTitleTextFont;
    [_containerView addSubview:_transL];
    
    _transTF                   = [[UITextField alloc] init];
    _transTF.borderStyle       = UITextBorderStyleNone;
    _transTF.layer.borderColor = [[UIColor lightGrayColor] CGColor];
    _transTF.layer.borderWidth = 1.0;
    [_containerView addSubview:_transTF];
    
    _transBtn = [[UIButton alloc] init];
    [_containerView addSubview:_transBtn];
    
    _directionL         = [[UILabel alloc] init];
    _directionL.text    = @"行车方向：";
    _directionL.font    = KTitleTextFont;
    [_containerView addSubview:_directionL];
    
    _directionTF                   = [[UITextField alloc] init];
    _directionTF.borderStyle       = UITextBorderStyleNone;
    _directionTF.layer.borderColor = [[UIColor lightGrayColor] CGColor];
    _directionTF.layer.borderWidth = 1.0;
    [_containerView addSubview:_directionTF];
    
    _directionBtn = [[UIButton alloc] init];
    [_containerView addSubview:_directionBtn];
    
    _pileL         = [[UILabel alloc] init];
    _pileL.text    = @"桩      号：";
    _pileL.font    = KTitleTextFont;
    [_containerView addSubview:_pileL];
    
    _beginPileTF                   = [[UITextField alloc] init];
    _beginPileTF.keyboardType      = UIKeyboardTypeNumberPad;
    _beginPileTF.borderStyle       = UITextBorderStyleNone;
    _beginPileTF.layer.borderColor = [[UIColor lightGrayColor] CGColor];
    _beginPileTF.layer.borderWidth = 1.0;
    [_containerView addSubview:_beginPileTF];
    
    _lineV = [[UIView alloc] init];
    [_containerView addSubview:_lineV];
    
    _endPileTF                   = [[UITextField alloc] init];
    _endPileTF.keyboardType      = UIKeyboardTypeNumberPad;
    _endPileTF.borderStyle       = UITextBorderStyleNone;
    _endPileTF.layer.borderColor = [[UIColor lightGrayColor] CGColor];
    _endPileTF.layer.borderWidth = 1.0;
    [_containerView addSubview:_endPileTF];
    
    _canleBtn     = [[UIButton alloc] init];
//    _canleBtn.tag = 0;
    [_canleBtn setBackgroundImage:[UIImage imageNamed:@"取消按钮.png"] forState:UIControlStateNormal];
    [_canleBtn setTitle:@"取   消" forState:UIControlStateNormal];
    [_containerView addSubview:_canleBtn];
    
    _confirmBtn     = [[UIButton alloc] init];
//    _confirmBtn.tag = 1;
    [_confirmBtn setBackgroundImage:[UIImage imageNamed:@"确认按钮"] forState:UIControlStateNormal];
    [_confirmBtn setTitle:@"确   认" forState:UIControlStateNormal];
    [_containerView addSubview:_confirmBtn];
    
    
    // K
    UILabel *label1           = [[UILabel alloc] init];
    label1.text               = @" K";
    label1.frame              = CGRectMake(0, 0, 15, 30);
    label1.font               = KTitleTextFont;
    _beginPileTF.leftView     = label1;
    _beginPileTF.leftViewMode = UITextFieldViewModeAlways;
    
    UILabel *label2           = [[UILabel alloc] init];
    label2.text               = @" K";
    label2.font               = KTitleTextFont;
    label2.frame              = CGRectMake(0, 0, 15, 30);
    _endPileTF.leftView       = label2;
    _endPileTF.leftViewMode   = UITextFieldViewModeAlways;
}

- (void) layoutUI {
    __weak __typeof(self)weakSelf = self;
    
    [_containerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(weakSelf.mas_centerX);
        make.centerY.mas_equalTo(weakSelf.mas_centerY);
        make.left.mas_equalTo(weakSelf.mas_left).with.offset(20);
        make.right.mas_equalTo(weakSelf.mas_right).with.offset(-20);
        make.height.mas_equalTo(270);
    }];
    
    [_roadL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(weakSelf.containerView.mas_left).with.offset(20);
        make.height.mas_equalTo(30);
        make.width.mas_equalTo(80);
        make.top.mas_equalTo(weakSelf.containerView.mas_top).with.offset(20);
    }];
    
    [_roadTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(weakSelf.roadL.mas_right).with.offset(5);
        make.right.mas_equalTo(weakSelf.containerView.mas_right).with.offset(-20);
        make.height.mas_equalTo(30);
        make.centerY.mas_equalTo(weakSelf.roadL.mas_centerY);
    }];
    
    [_roadBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(weakSelf.roadTF.mas_top);
        make.left.mas_equalTo(weakSelf.roadTF.mas_left);
        make.right.mas_equalTo(weakSelf.roadTF.mas_right);
        make.bottom.mas_equalTo(weakSelf.roadTF.mas_bottom);
    }];
    //
    [_transL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(weakSelf.containerView.mas_left).with.offset(20);
        make.height.mas_equalTo(30);
        make.width.mas_equalTo(80);
        make.top.mas_equalTo(weakSelf.roadL.mas_bottom).with.offset(20);
    }];
    
    [_transTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(weakSelf.transL.mas_right).with.offset(5);
        make.right.mas_equalTo(weakSelf.containerView.mas_right).with.offset(-20);
        make.height.mas_equalTo(30);
        make.centerY.mas_equalTo(weakSelf.transL.mas_centerY);
    }];
    
    [_transBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(weakSelf.transTF.mas_top);
        make.left.mas_equalTo(weakSelf.transTF.mas_left);
        make.right.mas_equalTo(weakSelf.transTF.mas_right);
        make.bottom.mas_equalTo(weakSelf.transTF.mas_bottom);
    }];
    
    
    //
    [_directionL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(weakSelf.containerView.mas_left).with.offset(20);
        make.height.mas_equalTo(30);
        make.width.mas_equalTo(80);
        make.top.mas_equalTo(weakSelf.transL.mas_bottom).with.offset(20);
    }];
    
    [_directionTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(weakSelf.directionL.mas_right).with.offset(5);
        make.right.mas_equalTo(weakSelf.containerView.mas_right).with.offset(-20);
        make.height.mas_equalTo(30);
        make.centerY.mas_equalTo(weakSelf.directionL.mas_centerY);
    }];
    
    [_directionBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(weakSelf.directionTF.mas_top);
        make.left.mas_equalTo(weakSelf.directionTF.mas_left);
        make.right.mas_equalTo(weakSelf.directionTF.mas_right);
        make.bottom.mas_equalTo(weakSelf.directionTF.mas_bottom);
    }];
    
    
    [_pileL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(weakSelf.containerView.mas_left).with.offset(20);
        make.height.mas_equalTo(30);
        make.width.mas_equalTo(80);
        make.top.mas_equalTo(weakSelf.directionL.mas_bottom).with.offset(20);
    }];
    
    [_beginPileTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(weakSelf.pileL.mas_centerY);
        make.height.mas_equalTo(30);
        make.left.mas_equalTo(weakSelf.pileL.mas_right).with.offset(5);
        make.right.mas_equalTo(weakSelf.endPileTF.mas_left).with.offset(-10);
        make.width.mas_equalTo(weakSelf.endPileTF.mas_width);
    }];
    
    [_lineV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(weakSelf.beginPileTF.mas_right);
        make.centerY.mas_equalTo(weakSelf.pileL.mas_centerY);
        make.width.mas_equalTo(10);
        make.height.mas_equalTo(1);
    }];
    
    [_endPileTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(weakSelf.lineV.mas_right);
        make.right.mas_equalTo(weakSelf.containerView.mas_right).with.offset(-20);
        make.height.mas_equalTo(30);
        make.centerY.mas_equalTo(weakSelf.pileL.mas_centerY);
    }];
    
    [_canleBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(weakSelf.containerView.mas_left).with.offset(20);
        make.height.mas_equalTo(30);
        make.width.mas_equalTo(weakSelf.confirmBtn.mas_width);
        make.top.mas_equalTo(weakSelf.pileL.mas_bottom).with.offset(20);
    }];
    
    [_confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(weakSelf.containerView.mas_right).with.offset(-20);
        make.height.mas_equalTo(30);
        make.left.mas_equalTo(weakSelf.canleBtn.mas_right).with.offset(30);
        make.centerY.mas_equalTo(weakSelf.canleBtn.mas_centerY);
    }];
}

- (void) btnClick {
    [_roadBtn addTarget:self action:@selector(roadBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [_transBtn addTarget:self action:@selector(transBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [_directionBtn addTarget:self action:@selector(directionBtnClick:) forControlEvents:UIControlEventTouchUpInside];
//    [_canleBtn addTarget:self action:@selector(cancelBtnClick:) forControlEvents:UIControlEventTouchUpInside];
//    [_confirmBtn addTarget:self action:@selector(confirmBtnClick:) forControlEvents:UIControlEventTouchUpInside];
}
#pragma mark - btnClick
- (void) roadBtnClick:(UIButton *)btn {
     AppDelegate *mydelegate             = (AppDelegate *)[UIApplication sharedApplication].delegate;
    __weak __typeof(self)weakSelf  = self;
    DrowDownViewController *roadVC = [[DrowDownViewController alloc]init];
    roadVC.view.width              = _roadBtn.width;
    roadVC.dataArray               = _roadNameArray;
    roadVC.view.height             = 44 * (_roadNameArray.count) + 10;
    _dropBackV.contentController   = roadVC;
    roadVC.indexClick              = ^(NSInteger tag, NSString *str){
        weakSelf.roadTF.text       = [NSString stringWithFormat:@"  %@",str];
        [_dropBackV dismiss];
        _currentRoadModel          = mydelegate.HighWayRoadArray[tag];
        [self setTransDirection];
    };
    [_dropBackV showFrom:_roadBtn]; }

- (void) transBtnClick:(UIButton *)btn {
    __weak __typeof(self)weakSelf   = self;
    DrowDownViewController *transVC = [[DrowDownViewController alloc]init];
    transVC.view.width              = _transBtn.width;
    transVC.dataArray               = _transTypeArray;
    transVC.view.height             = 44 * (_transTypeArray.count) + 10;
    _dropBackV.contentController    = transVC;
    transVC.indexClick              = ^(NSInteger tag, NSString *str){
        weakSelf.transTF.text       = [NSString stringWithFormat:@"  %@",str];
        [_dropBackV dismiss];
    };
    [_dropBackV showFrom:_transBtn];
}

- (void) directionBtnClick:(UIButton *)btn {
    __weak __typeof(self)weakSelf       = self;
    DrowDownViewController *directionVC = [[DrowDownViewController alloc]init];
    directionVC.view.width              = _directionBtn.width;
    directionVC.dataArray               = _roadDirectionArray;
    directionVC.view.height             = 44 * (_roadDirectionArray.count) + 10;
    _dropBackV.contentController        = directionVC;
    directionVC.indexClick              = ^(NSInteger tag, NSString *str){
        weakSelf.directionTF.text       = [NSString stringWithFormat:@"  %@",str];
        _directionNum                   = _roadDirectionNumArray[tag];
        [_dropBackV dismiss];
    };
    [_dropBackV showFrom:_directionBtn];
}

//-(void) cancelBtnClick:(UIButton *)btn{
//    [self dimissInquiryView];
//}

#pragma mark -联动方向
- (void) setTransDirection {
    [_roadDirectionArray  removeAllObjects];
    for (HighWayRoadRecordDirectionInfoModel *directModel in _currentRoadModel.highWayRoadDirectionList) {
        [_roadDirectionArray addObject:directModel.DirectionDescription];
    }
    _directionTF.text = [NSString stringWithFormat:@"  %@",_roadDirectionArray[0]];
}
#pragma mark - 数据筛选
- (void) configDefatulArrayData {
    AppDelegate *mydelegate                 = (AppDelegate *)[UIApplication sharedApplication].delegate;
    //    _defaultRoadStr                         = roadModel.RoadName;
    for (HighwayMergeRoadRecordModel *roadModel in mydelegate.HighWayRoadArray) {
        [_roadNameArray addObject:roadModel.RoadName]; // 道路类型数组
       
    }
    //  方向数组
    for (HighWayRoadRecordDirectionInfoModel *directModel in _currentRoadModel.highWayRoadDirectionList) {
        [_roadDirectionArray addObject:directModel.DirectionDescription];
         [_roadDirectionNumArray addObject:directModel.RoadDirection];
    }
    
    //车辆类型
    _transTypeArray = @[@"全部车辆", @"营运客车", @"危化品车"];
}
#pragma mark -默认显示的数组
- (void) setDefaultShowData {
    _roadTF.text      = [NSString stringWithFormat:@"  %@", _roadNameArray[0]]; //默认的道路
    _directionTF.text = [NSString stringWithFormat:@"  %@", _roadDirectionArray[0]];//对应默认的道路的方向
    _directionNum     = _roadDirectionNumArray[0] ;
    _transTF.text     = [NSString stringWithFormat:@"  %@", _transTypeArray[0]];
}



#pragma mark - 退出
- (void)dimissInquiryView {
    [self removeFromSuperview];
}
//- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
//    [self dimissInquiryView];
//}

@end
